package com.yuhang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyBill;
import com.yuhang.entity.HyComplaints;
import com.yuhang.entity.HyUser;
import com.yuhang.entity.StateMonth;
import com.yuhang.service.HyCarService;
import com.yuhang.service.HyComplaintsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/hy-complaints")
public class HyComplaintsController {

    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;

    @Autowired
    private HyComplaintsService hyComplaintsService;

    @RequestMapping("/showAllComplaints")
    public Map<String, Object> shoAllComplaints(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                                @RequestParam(value = "limit", defaultValue = "5") int pageSize,
                                                @RequestParam(value = "likename",defaultValue = "")String likename) {
        IPage<HyComplaints> page = hyComplaintsService.selectByPage(pageNum, pageSize,likename);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", page.getTotal());
        map.put("data", page.getRecords());
        map.put("msg", "");
        return map;
    }

    @RequestMapping("/showAllUserComplaints")
    public Map<String, Object> showAllUserComplaints(HttpSession session,
                                                     @RequestParam(value = "page", defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "limit", defaultValue = "5") int pageSize,
                                                     @RequestParam(value = "likename", defaultValue = "") String likename) {
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        IPage<HyComplaints> page = hyComplaintsService.selectByPage(pageNum, pageSize, likename,hyUser.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", page.getTotal());
        map.put("data", page.getRecords());
        map.put("msg", "");
        return map;
    }

    @RequestMapping("/addComplaints")
    public Map<String, Object> addHyComplaints(HyComplaints hyComplaints) {
        hyComplaints.setState(1);
        hyComplaints.setEvaluate(1);//1-匿名，0-不匿名
        hyComplaintsService.save(hyComplaints);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }
    @RequestMapping("/addUserComplaints")
    public Map<String, Object> addUserComplaints(HttpSession session,HyComplaints hyComplaints) {
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        hyComplaints.setUserId(hyUser.getId());
        hyComplaints.setUserName(hyUser.getName());
        hyComplaints.setState(1);
        hyComplaintsService.save(hyComplaints);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    @RequestMapping("/updateComplaints")
    public Map<String, Object> updateHyComplaints(HyComplaints hyComplaints) {
        hyComplaintsService.updateById(hyComplaints);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    @RequestMapping("/deleteComplaints")
    public Map<String, Object> deleteHyComplaints(Integer id) {
        hyComplaintsService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    @RequestMapping("/uploadImage")
    public Map<String, Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path, file.getOriginalFilename()));
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", file.getOriginalFilename());
        return map;
    }

    @RequestMapping("/shouliComplaints")
    public Map<String, Object> shouliComplaints(Integer id) {
        HyComplaints hyComplaints = hyComplaintsService.getById(id);
        hyComplaints.setState(2);
        hyComplaintsService.updateById(hyComplaints);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }
    @RequestMapping("/pingjiaComplaints")
    public Map<String, Object> pingjiaComplaints(HyComplaints hyComplaints) {
        HyComplaints hyComplaints1 = hyComplaintsService.getById(hyComplaints);
        hyComplaints1.setState(3);
        hyComplaints1.setEvaluate(hyComplaints.getEvaluate());
        hyComplaintsService.updateById(hyComplaints1);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

}
